﻿<?php
require_once('../../../config.php');
 require_once($CFG->libdir . '/adminlib.php');
 require_once($CFG->dirroot . '/local/myplugin/lib.php');
require_once($CFG->dirroot . "/local/myplugin/forms.php");

require_login($SITE);

if (!is_siteadmin()) {
    print_error("vui lòng đăng nhập với quyền admin để thao tác");
}
global $DB;
$option=array();

/* Ket noi den file javascript - module.js */
$PAGE->requires->js_init_call('M.local_myplugin');	
$cssdir =  $CFG->wwwroot.'/local/myplugin/css/mycss.css';
echo '<link rel="stylesheet" href="'.$cssdir.'" type="text/css" media="screen" />';
echo '<script src="../colorbox/jquery.min.js"></script>';
echo '<script src="../module.js"></script>';
/* Chuan bi trang web can hien thi */	
$url = new moodle_url($CFG->wwwroot.'local/myplugin/lo/add_lo.php');
$PAGE->set_url($url);
$PAGE->set_title('Add Academic Program');
$PAGE->set_heading('Add Academic Program');
$PAGE->set_pagelayout('standard');

//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($CFG->wwwroot.'/local/myplugin/lo/add_lo.php', array('param'=>'value')));

echo $OUTPUT->header();

/* Viet doan code cua ban o day */

//add tab vào trang
$mylib = new learning_outcome_management();
//set tab hien tai
echo "<script  type='text/javascript'>success_mess('123');</script>";
$add_curr_form = new form_add_lo();
$add_curr_form->display();
//bat su kien nhan nu cancel
if ($add_curr_form->is_cancelled()) {

echo 'hủy thao tác';

}

//kiem tra neu dc submitted thì làm gì
if ($fromform = $add_curr_form->get_data()) 
{
	$program_id=$fromform->cblo_program;
	$parent_id=$_POST['cbparentlevel'];
	$sql="SELECT max(lo_id) as abc FROM mdl_learning_outcome WHERE parent_lo_id='".$parent_id."' and program_id='".$program_id."'";
	$max_id="";
	if($ids=$DB->get_records_sql($sql))
	{
		foreach($ids as $id)
		{
			$max_id =$id->abc;
		}
	}	
	$last=1;
	$level=count(explode('.', $parent_id))+$last;
	$option['lo_id']=generate_lo_id($parent_id,$max_id, $program_id, $level);
	$option['lo_name']=$fromform->txtten;
	$option['lo_level']=$level;
	$option['parent_lo_id']=$parent_id;
	$option['program_id']=$program_id;
	$option['status']='1';
	$mylib->add_new_lo($option);
	echo "<script  type='text/javascript'>success_mess('Learning Outcome Is Added Successfully!');</script>";
	echo "<script  type='text/javascript'>clear_form_elements('#mform1');</script>";
}

function generate_lo_id($parent_id,$max_id, $program_id, $level)
{
	if($max_id=='')
	{
		//kiem tra node la thi ko tra id lai
		global $DB;
		//lay max level cua lo
		$sql="SELECT max(lo_level) as maxlevel FROM mdl_learning_outcome WHERE program_id='".$program_id."'";
		$max_level="";
		if($ids=$DB->get_records_sql($sql))
		{
			foreach($ids as $id)
			{
				$max_level =$id->maxlevel;
			}
		}
		if($level<$max_level)
		{
			return $parent_id.'.1';
		}
		else		
		{
			return '';
		}
	}
	else
	{
		//khuc này cat chuoi de lấy phần dầu của số cần tang, ví du maxid là 1.2.3 thì ở day cắt lấy chuoi 1.2. để lát ghép vào sau khi tăng số 3 thành so 4
		$cons=1;
		$pre_id= substr($max_id, 0, lastIndexOf('.',$max_id));
		$last_id= substr($max_id, lastIndexOf('.',$max_id))+$cons;
		return $pre_id.$last_id;
	}
}
function lastIndexOf($string,$item){  
		$index=strpos(strrev($string),strrev($item));  
		if ($index){  
			$index=strlen($string)-strlen($item)-$index;  
			return $index;  
		}  
			else  
			return -1;  
	}

/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();

